
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;

public class SaveManager : MonoBehaviour
{
    //单例模式
    public static SaveManager instance;
    public void Awake()
    {
        instance=this;
    }
    //MainData接口列表
    private List<ISaveMainManager> iSaveMainManager = new List<ISaveMainManager>();
    //AchiData接口列表
    private List<ISaveAchiManager> iSaveAchiManager = new List<ISaveAchiManager>();
    //LevelData接口列表
    public List<ISaveLevelManager> iSaveLevelManager = new List<ISaveLevelManager>();
    //ArtifactData接口列表
    private List<ISaveArtifact> iSaveArtifact = new List<ISaveArtifact>();
    //Random接口列表
    private List<ISaveRandomManager> iSaveRandomManager = new List<ISaveRandomManager>();
    [Header("Main数据")]
    public MainData mainData;
    [Header("Achi数据")]
    public AchiData achiData;
    [Header("Level数据")]
    public LevelData levelData;
    [Header("Artifact数据")]
    public ArtifactData artifactData;
    [Header("Random数据")]
    public RandomData randomData;

    //找到MainData接口
    public void SeekMainDataInterface()
    {
        iSaveMainManager.Clear();
        IEnumerable<ISaveMainManager> mainManagers = FindObjectsOfType<MonoBehaviour>().OfType<ISaveMainManager>();
        iSaveMainManager=new List<ISaveMainManager>(mainManagers);
    }
    //找到AchiData接口
    public void SeekAchiDataInterface()
    {
        iSaveAchiManager.Clear();
        IEnumerable<ISaveAchiManager> achiManagers = FindObjectsOfType<MonoBehaviour>().OfType<ISaveAchiManager>();
        iSaveAchiManager=new List<ISaveAchiManager>(achiManagers);
    }
    //找到LevelData接口
    public void SeekLevelDataInterface()
    {
        iSaveLevelManager.Clear();
        IEnumerable<ISaveLevelManager> levelManagers = FindObjectsOfType<MonoBehaviour>().OfType<ISaveLevelManager>();
        iSaveLevelManager=new List<ISaveLevelManager>(levelManagers);
    }
    //找到Artifact接口
    public void SeekArtifactDataInterface()
    {
        iSaveArtifact.Clear();
        IEnumerable<ISaveArtifact> artifacts = FindObjectsOfType<MonoBehaviour>().OfType<ISaveArtifact>();
        iSaveArtifact=new List<ISaveArtifact>(artifacts);
    }
    //找到Random接口
    public void SeekRandomDataInterface()
    {
        iSaveRandomManager.Clear();
        IEnumerable<ISaveRandomManager> randoms = FindObjectsOfType<MonoBehaviour>().OfType<ISaveRandomManager>();
        iSaveRandomManager=new List<ISaveRandomManager>(randoms);
    }
    //调用MainData接口获取
    public void GetMainDataInterface()
    {
        foreach(var i in iSaveMainManager)
        {
            i.Load(mainData);
        }
    }
    //调用MainData接口保存
    public void SaveMainDataInterface()
    {
        foreach (var i in iSaveMainManager)
        {
            i.Save(ref mainData);
        }
    }
    //调用AchiData接口获取
    public void GetAchiInterface()
    {
        foreach (var i in iSaveAchiManager)
        {
            i.Load(achiData);
        }
    }
    //调用AchiData接口保存
    public void SaveAchiInterface()
    {
        foreach (var i in iSaveAchiManager)
        {
            i.Save(ref achiData);
        }
    }
    //调用LevelData接口获取
    public void GetLevelDataInterface()
    {
        foreach (var i in iSaveLevelManager)
        {
            i.Load(levelData);
        }
    }
    //调用LevelData接口保存
    public void SaveLevelDataInterface()
    {
        foreach (var i in iSaveLevelManager)
        {
            i.Save(ref levelData);
        }
    }
    //调用artifactData接口获取
    public void GetArtifactInterface()
    {
        foreach (var i in iSaveArtifact)
        {
            i.Load(artifactData);
        }
    }
    //调用artifactData接口保存
    public void SaveArtifactInterface()
    {
        foreach (var i in iSaveArtifact)
        {
            i.Save(ref artifactData);
        }
    }
    //调用RandomData接口获取
    public void GetRandomInterface()
    {
        foreach (var i in iSaveRandomManager)
        {
            i.Load(randomData);
        }
    }
    //调用RandomData接口保存
    public void SaveRandomInterface()
    {
        foreach (var i in iSaveRandomManager)
        {
            i.Save(ref randomData);
        }
    }
    //保存游戏核心数据
    public void SaveMainData(string filePath,string fileName)
    {
        //foreach(var i in iSaveMainManager)
        //{
        //    i.Save( ref mainData);
        //}
        FileHandler.Save(filePath, fileName,mainData);
    }
    //获取游戏核心数据
    public void LoadMainData(string filePath, string fileName)
    {       
        mainData=FileHandler.Load<MainData>(filePath, fileName);

        foreach (var i in iSaveMainManager)
        {
            i.Load(mainData);
        }
    }
    //保存成就数据
    public void SaveAchiData(string filePath, string fileName)
    {
        //foreach (var i in iSaveAchiManager)
        //{
        //    i.Save(ref achiData);
        //}
        FileHandler.Save(filePath, fileName, achiData);
    }
    //获取成就数据
    public void LoadAchiData(string filePath, string fileName)
    {
        achiData=FileHandler.Load<AchiData>(filePath, fileName);

        //foreach (var i in iSaveAchiManager)
        //{
        //    i.Load(achiData);
        //}
    }
    //保存层级数据
    public void SaveLevelData(string filePath, string fileName)
    {
        //foreach (var i in iSaveLevelManager)
        //{
        //    i.Save(ref levelData);
        //}
        FileHandler.Save(filePath, fileName, levelData);
    }
    //获取层级数据
    public void LoadLevelData(string filePath, string fileName)
    {       
        levelData=FileHandler.Load<LevelData>(filePath, fileName);
        //foreach (var i in iSaveLevelManager)
        //{
        //    i.Load(levelData);
        //}
    }
    //保存神器数据
    public void SaveArtifactData(string filePath, string fileName)
    {
        //foreach (var i in iSaveArtifact)
        //{
        //    i.Save(ref artifactData);
        //}
        FileHandler.Save(filePath, fileName, artifactData);
    }
    //获取神器数据
    public void LoadArtifactData(string filePath, string fileName)
    {
        artifactData=FileHandler.Load<ArtifactData>(filePath, fileName);
        //foreach (var i in iSaveArtifact)
        //{
        //    i.Load(artifactData);
        //}
    }
    //保存Random
    public void SaveRandomData(string filePath, string fileName)
    {
        //foreach (var i in iSaveArtifact)
        //{
        //    i.Save(ref artifactData);
        //}
        FileHandler.Save(filePath, fileName, randomData);
    }
    //获取Random
    public void LoadRandomData(string filePath, string fileName)
    {
        randomData=FileHandler.Load<RandomData>(filePath, fileName);
        //foreach (var i in iSaveArtifact)
        //{
        //    i.Load(artifactData);
        //}
    }
    //游戏重置(保留成就)(游戏内的死亡，通关都会保留成就，成就无奖励)
    public void ReSetGameData()
    {
        //直接删掉文件夹
        string filePath = Path.Combine(Application.persistentDataPath, "Data");
        //文件存在吗
        if(!Directory.Exists(filePath))
        {
            UnityEngine.Debug.Log("数据文件不存在");
            return;
        }
        //遍历删除
        string[] allFile = Directory.GetFiles(filePath);
        foreach(string i in allFile)
        {
            string fileName = Path.GetFileName(i);
            if(fileName!="AchiData")
            {
                File.Delete(i);
            }
        }
    }
    //退出游戏
    private void OnApplicationQuit()
    {
        //正在战斗的时候退出无法保存
        if (GameManager.Instance.isBattle) return;
        SaveAllData();
    }
    //切出后台保存数据
    private void OnApplicationPause(bool pause)
    {
        if(pause)
        {
            //正在战斗的时候退出无法保存
            if (GameManager.Instance.isBattle) return;
            SaveAllData();
        }
    }
    //保存数据
    public void SaveAllData()
    {
        //如果主角血量为0和负数，无法保存，游戏是不能小于等于0的
        if (GameManager.Instance.player.currHp <= 0) return;
        //这里需要保存一下所有数据
        //保存主数据
        SeekMainDataInterface();
        SaveMainDataInterface();
        SaveMainData(Application.persistentDataPath, "Data"+GameManager.Instance.fileNum+"/MainData");

        if (GameManager.Instance.currLevelObj!=null)
        {
            //保存层级数据
            SeekLevelDataInterface();
            SaveLevelDataInterface();
            SaveLevelData(Application.persistentDataPath, "Data"+GameManager.Instance.fileNum+"/Level"+SceneItemManager.instance.id);
        }

        //保存神器数据
        SeekArtifactDataInterface();
        SaveArtifactInterface();
        SaveArtifactData(Application.persistentDataPath, "Data"+GameManager.Instance.fileNum+"/ArtifactData");

        //保存成就数据
        SeekAchiDataInterface();
        SaveAchiInterface();
        SaveAchiData(Application.persistentDataPath, "Data"+GameManager.Instance.fileNum+"/AchiData");

        if(RandomRoomManager.instance!=null)
        {
            //保存Random数据
            SeekRandomDataInterface();
            SaveRandomInterface();
            SaveRandomData(Application.persistentDataPath, "Data"+GameManager.Instance.fileNum+"/RandomData"+RandomRoomManager.instance.saveId);
        }
    }
    //死亡加载数据
    public void DieLoadData()
    {
        //加载主数据
        LoadMainData(Application.persistentDataPath, "Data" + GameManager.Instance.fileNum + "/MainData");
        SeekMainDataInterface();
        GetMainDataInterface();
        //加载神器数据
        LoadArtifactData(Application.persistentDataPath, "Data" + GameManager.Instance.fileNum + "/ArtifactData");
        SeekArtifactDataInterface();
        GetArtifactInterface();
        //加载层级数据
        LoadLevelData(Application.persistentDataPath, "Data" + GameManager.Instance.fileNum + "/Level" + SceneItemManager.instance.id);
        SeekLevelDataInterface();
        GetLevelDataInterface();

        //加载成就数据
        LoadAchiData(Application.persistentDataPath, "Data" + GameManager.Instance.fileNum + "/AchiData");
        SeekAchiDataInterface();
        GetAchiInterface();

        if(RandomRoomManager.instance!=null)
        {
            //加载Random数据
            LoadRandomData(Application.persistentDataPath, "Data" + GameManager.Instance.fileNum + "/RandomData" + RandomRoomManager.instance.saveId);
            SeekRandomDataInterface();
            GetRandomInterface();
        }
    }
    //删除数据
    public void DeleteAllData(int num)
    {
        //拼接路径
        string fullPath = Path.Combine(Application.persistentDataPath, "Data"+num);
        if(Directory.Exists(fullPath))
        {
            //删除文件夹，包括里面的数据
            Directory.Delete(fullPath, true);
        }
        else
        {
            Debug.Log("不存在此文件夹");
        }
    }
}